package com.kz.bot.service;

import com.alibaba.fastjson2.JSONObject;
import com.kz.bot.api.ApiRequest;
import com.kz.bot.constant.Side;
import com.kz.bot.model.Order;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
@Slf4j
public class ApiService {

    private final ApiRequest apiRequest;

    /**
     * 查询当前委托单
     * @param instId 币种
     * @param side 方向
     * @return
     */
    public List<Order> currentOrder(String instId, Side side) {
        List<Order> list = new ArrayList<>();
        try {
            List<JSONObject> orderJSON = apiRequest.pendingOrders(instId);
            for (JSONObject orderJson : orderJSON) {
                String ordId = orderJson.getString("ordId");
                String ordSide = orderJson.getString("side");
                String px = orderJson.getString("px");
                if (side.name().equals(ordSide)) {
                    Order order = Order.builder()
                            .ordId(ordId)
                            .side(ordSide)
                            .price(new BigDecimal(px))
                            .build();
                    list.add(order);
                }
            }
        } catch (Exception e) {
            log.error("查询当前买单失败", e);
        }
        return list;
    }

    public Order placeOrder(String instId, Side side, BigDecimal volume, BigDecimal price) {
        try {
            JSONObject jsonObject = apiRequest.createOrder(instId, side, volume.toString(), price.toString());
            String ordId = jsonObject.getString("ordId");
            Order order = new Order(ordId, side.name(), price, volume);
            return order;
        } catch (Exception e) {
            log.error("下单失败", e);
        }
        return null;
    }

    public BigDecimal queryLastPrice(String instId) {
        try {
            String price = apiRequest.queryLastPrice(instId);
            return new BigDecimal(price);
        } catch (Exception e) {
            log.error("查询最新价格异常", e);
        }
        return null;
    }

    public String queryOrderStatus(String instId, String ordId) {
        try {
            JSONObject jsonObject = apiRequest.queryOrderStatus(instId, ordId);
            return jsonObject.getString("state");
        } catch (Exception e) {
            log.error("查询订单状态失败", e);
        }
        return null;
    }

    /**
     * 查询可用余额
     * @param currency 币种
     * @return 可用余额
     */
    public BigDecimal queryAccountBalance(String currency) {
        return apiRequest.queryAccountBalance(currency);
    }

    /**
     * 撤单
     * @param instId 币种
     * @param ordId 订单id
     */
    public void cancelOrder(String instId, String ordId) {
        try {
            apiRequest.cancelOrder(instId, ordId);
        } catch (Exception e) {
            log.error("撤单失败", e);
        }
    }
}
